import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router";


const routes: Array<RouteRecordRaw> = [
  {
    path: "/",
    redirect: '/login'
  },
  {
    path: "/search",
    name: "search",
    component: () => import("../views/search/index.vue"),
    meta: { title: '搜索' }
  },
  {
    path: "/home",
    name: "home",
    component: () => import("../views/HomeView.vue"),
    children: [
      {
        path: "/home/index",
        name: "index",
        component: () => import("../views/home/Index.vue"),
        meta: { title: '首页' }
      },
      {
        path: "/home/exam",
        name: "exam",
        component: () => import("../views/search/exam.vue"),
        children: [
          {
            path: "/home/exam",
            redirect: "/home/exam/exam/pai", // 设置重定向
          },
          {
            path: "/home/exam/exam/pai",
            name: "pai",
            component: () => import("../views/search/examchild/pai.vue"),
            meta: { title: '答题排行榜' }
          },
          {
            path: "/home/exam/exam/lian",
            name: "lian",
            component: () => import("../views/search/examchild/lian.vue"),
            meta: { title: '党建知识练习' }
          },
          {
            path: "/home/exam/exam/ceping",
            name: "ceping",
            component: () => import("../views/search/examchild/ceping.vue"),
            meta: { title: '党建知识测评' }
          },
          {
            path: "/home/exam/exam/jilu",
            name: "jilu",
            component: () => import("../views/search/examchild/jilu.vue"),
            meta: { title: '我的测评记录' }
          },
        ]
      },
      {
        path: "/home/openexam",
        name: "openexam",
        component: () => import("../views/search/examchild/openexam.vue"),
        meta: { title: '开始练习' }
      },
      {
        path: "/home/openceshi",
        name: "openceshi",
        component: () => import("../views/search/examchild/openceshi.vue"),
        meta: { title: '开始测试' }
      },
      {
        path: "/home/examresult",
        name: "examresult",
        component: () => import("../views/search/examchild/examresult.vue"),
        meta: { title: '测试结果' }
      },
      {
        path: "/home/lianresult",
        name: "lianresult",
        component: () => import("../views/search/examchild/lianresult.vue"),
        meta: { title: '练习结果' }
      },
      {
        path: "/home/team-t",
        name: "team-t",
        component: () => import("../views/home/Team-t.vue"),
        meta: { title: '支部风采' },
        // children: [
        //   {
        //     path: "/home/team-t/detail",
        //     name: "detail",
        //     component: () => import("../views/team/Onedetail.vue"),
        //     meta: { title: '支部风采详情' }
        //   }
        // ]
      },
      {
        path: "/home/team-t/detail",
        name: "detail",
        component: () => import("../views/team/Onedetail.vue"),
        meta: { title: '动态详情' }
      },
      {
        path: "/home/team-d",
        name: "team-d",
        component: () => import("../views/home/Team-d.vue"),
        meta: { title: '党员风采' }
      },
      {
        path: "/home/home/team-d/onecen",
        name: "onecen",
        component: () => import("../views/team-d/Onecen.vue"),
        meta: { title: '党员风采详情' }
      },
      {
        path: "/home/workbenck",
        name: "workbenck",
        component: () => import("../views/home/Workbenck.vue"),
        meta: { title: '党务工作台' }
      },
      {
        path: "/home/publicbar",
        name: "publicbar",
        component: () => import("../views/home/Publicbar.vue"),
        meta: { title: '党务公开栏' }
      },
      {
        path: "/home/publicbar/publicchild/process",
        name: "process",
        component: () => import("../views/publicchild/process.vue"),
        meta: { title: '党务公开栏详情' }
      },
      {
        path: "/home/alermbells",
        name: "alermbells",
        component: () => import("../views/home/Alermbells.vue"),
        meta: { title: '党纪警示钟' }
      },
      {
        path: "/home/msgboards",
        name: "msgboards",
        component: () => import("../views/home/Msgboards.vue"),
        meta: { title: '党员留言板' }
      },

      // 学习矩阵
      {
        path: "/home/study",
        name: "study",
        component: () => import("../views/search/study.vue"),
        meta: { title: '学习矩阵' }
      },
      // 学习矩阵文章详情
      {
        path: "/home/particulars",
        name: "particulars",
        component: () => import("../views/search/particulars.vue"),
        meta: { title: '学习矩阵文章详情' }
      },
      // 最美网文
      {
        path: "/home/article",
        name: "article",
        component: () => import("../views/search/article.vue"),
        meta: { title: '最美网文' }
      },
      // 最美网文文章详情
      {
        path: "/home/articles",
        name: "articles",
        component: () => import("../views/search/articles.vue"),
        meta: { title: '最美网文文章详情' }
      },
      // 视频相关
      {
        path: "/home/frequency",
        name: "frequency",
        component: () => import("../views/frequency/frequency.vue"),
        meta: { title: '搜索' }
      },
      // 个人中心
      {
        path: "/home/personage",
        name: "personage",
        component: () => import("../views/personage/personage.vue"),
        children: [
          {
            path: "/home/personage/centre",
            name: "centre",
            component: () => import("../views/personage/centre.vue"),
            meta: { title: '个人信息' }
          },
          {
            path: "/home/personage/leave",
            name: "leave",
            component: () => import("../views/personage/leave.vue"),
            meta: { title: '我的留言' }
          },
          {
            path: "/home/personage/discuss",
            name: "discuss",
            component: () => import("../views/personage/discuss.vue"),
            meta: { title: '我的评论' }
          },
          {
            path: "/home/personage/amend",
            name: "amend",
            component: () => import("../views/personage/amend.vue"),
            meta: { title: '修改密码' }
          },
          {
            path: "/home/personage/interspace",
            name: "interspace",
            component: () => import("../views/personage/interspace.vue"),
            meta: { title: '我的空间' }
          },
          {
            path: "/home/personage/audit",
            name: "audit",
            component: () => import("../views/personage/audit.vue"),
            meta: { title: '待我审核' }
          },
        ]
      },
    ]
  },
  {
    path: "/login",
    name: "login",
    component: () => import("../views/AboutView.vue"),
    meta: { title: '登录' }
  },
  // 理论之星
  {
    path: "/theory",
    name: "theory",
    component: () => import("../views/frequency/theory.vue"),
    meta: { title: '理论之星' }
  },
  {
    path: "/bigdata",
    name: "bigdata",
    component: () => import("../views/home/Bigdata.vue"),
    meta: { title: '党建大数据' }
  },
  {
    path: "/event",
    name: "event",
    component: () => import("../views/home/event.vue"),
    meta: { title: '数据统计中' }
  },

];

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
});
// 导航守卫，在每次路由切换前更新标题
const whiteList = ['/login',] //白名单
router.beforeEach((to, from, next) => {

  document.title = (to.meta.title as string) || '默认标题';
  let token = localStorage.getItem('token')
  if (token) {
    if (whiteList.includes(to.path)) {
      next('/home/index')
    } else {
      next()
    }
  } else {
    // 白名单验证
    if (whiteList.includes(to.path)) {
      next()
    } else {
      // alert('您还没有登录,请先去登录')
      next('/login')
      // router.replace('/login');
    }
  }
})

export default router;
